package com.example.login;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText name;
    EditText passwd;
    String user;
    String pwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
        setContentView(R.layout.activity_main);
         name =(EditText) findViewById(R.id.name);
         passwd = (EditText)findViewById(R.id.passwd);
        passwd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(!b){
                    user = name.getText().toString();
                    pwd = passwd.getText().toString();
                    if(user==null||user.equals("")){
                        Toast.makeText(MainActivity.this,"user null!",Toast.LENGTH_SHORT).show();
                    }else if(pwd==null||pwd.equals("")){
                        Toast.makeText(MainActivity.this,"pwd null!",Toast.LENGTH_SHORT).show();
                    }else{
                        MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    }
                }
            }
        });
//         new myrun().run();

        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                user = name.getText().toString();
                pwd = passwd.getText().toString();

                if(user==null||user.equals("")){
                    Toast.makeText(MainActivity.this,"user null!",Toast.LENGTH_SHORT).show();
                }else if(pwd==null||pwd.equals("")){
                    Toast.makeText(MainActivity.this,"pwd null!",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                    intent.putExtra("name", user);
                    intent.putExtra("passwd", pwd);
                    startActivity(intent);
                }


//               if (name.getText().equals("")||name.getText()==null) {
//                   Intent intent = new Intent(MainActivity.this, MainActivity2.class);
//                   intent.putExtra("name", name.getText().toString());
//                   intent.putExtra("passwd", passwd.getText().toString());
//                   startActivity(intent);
//               }else {
//                   Toast.makeText(MainActivity.this,"null!",Toast.LENGTH_SHORT).show();
//               }
            }
        });
        Intent startIntent = new Intent(MainActivity.this, Myservice.class);
        //服务服务
        findViewById(R.id.btn_start_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startService(startIntent);
            }
        });
        findViewById(R.id.btn_destroy_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopService(startIntent);
            }
        });


    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ){

            Toast.makeText(MainActivity.this,"竖屏模式",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(MainActivity.this,"h屏模式",Toast.LENGTH_SHORT).show();
        }
    }
    class myrun extends Thread{
        @Override
        public void run() {
            super.run();
            while (true){
                String name = MainActivity.this.name.getText().toString();
                String passwd =  MainActivity.this.passwd.getText().toString();
                System.out.println(name+"--"+passwd);

            }
        }
    }
}